package com.bing.bingPro.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bing.bingPro.controller.dto.TZwMeteringVelocityDto;
import com.bing.bingPro.controller.vo.TBorderLatestNsDetailsVo;
import com.bing.bingPro.controller.vo.TBorderLatestStatsVo;
import com.bing.bingPro.controller.vo.TBorderMeteringVo;
import com.bing.bingPro.entity.TBworderLstestNqDetails;
import com.bing.bingPro.entity.TBworderLstestNsDetails;
import com.bing.bingPro.entity.TBworderLstestStats;
import com.bing.bingPro.enums.TZwMeteringVelocityEnums;
import com.bing.bingPro.service.TBorderLatestNsDetailsService;
import com.bing.bingPro.mapper.TBworderLstestNsDetailsMapper;
import com.bing.bingPro.util.ProvinceUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static org.apache.commons.lang3.StringUtils.isNotEmpty;

/**
* @author wangqilong
* @description 针对表【t_bworder_lstest_ns_details】的数据库操作Service实现
*/
@Service
public class TBrowderLustiestNsDetailsServiceImpl extends ServiceImpl<TBworderLstestNsDetailsMapper, TBworderLstestNsDetails>
    implements TBorderLatestNsDetailsService {

    @Override
    public List<TBorderLatestNsDetailsVo> tBrowderNsDetailsList(TZwMeteringVelocityDto tZwMeteringVelocityDto) {
        String replace = tZwMeteringVelocityDto.getTime().replace("-", "");
        String province = tZwMeteringVelocityDto.getProvince();
        String orderType = ProvinceUtil.getOrderType(tZwMeteringVelocityDto.getOrderType());

        List<TBworderLstestNsDetails> statsList = baseMapper.selectList(Wrappers.lambdaQuery(TBworderLstestNsDetails.class)
                .eq(isNotEmpty(replace), TBworderLstestNsDetails::getDataTime, replace)
                .eq(isNotEmpty(orderType), TBworderLstestNsDetails::getOrderType, orderType)
                .eq(isNotEmpty(province), TBworderLstestNsDetails::getProvinceName, province));

        return statsList.stream()
                .map(tBorderLatestStats -> mapToTBorderLatestStatsVo(tBorderLatestStats, tZwMeteringVelocityDto.getTime()))
                .collect(Collectors.toList());
    }

    @Override
    public IPage<TBorderLatestNsDetailsVo> tBrowderNsDetailsPageList(TZwMeteringVelocityDto tZwMeteringVelocityDto) {
        String replace = tZwMeteringVelocityDto.getTime().replace("-", "");
        String province = tZwMeteringVelocityDto.getProvince();
        String orderType = ProvinceUtil.getOrderType(tZwMeteringVelocityDto.getOrderType());
        IPage<TBorderLatestNsDetailsVo> voIPage = new Page<>();
        Page<TBworderLstestNsDetails> page = new Page<>(tZwMeteringVelocityDto.getPageNum(), tZwMeteringVelocityDto.getPageSize());

        IPage<TBworderLstestNsDetails> pageResult = baseMapper.selectPage(page, Wrappers.lambdaQuery(TBworderLstestNsDetails.class)
                .eq(isNotEmpty(replace), TBworderLstestNsDetails::getDataTime, replace)
                .eq(isNotEmpty(orderType), TBworderLstestNsDetails::getOrderType, orderType)
                .eq(isNotEmpty(province), TBworderLstestNsDetails::getProvinceName, province));

        List<TBorderLatestNsDetailsVo> result = pageResult.getRecords().stream()
                .map(tBorderLatestStats -> mapToTBorderLatestStatsVo(tBorderLatestStats, tZwMeteringVelocityDto.getTime()))
                .collect(Collectors.toList());
        voIPage.setRecords(result);
        voIPage.setTotal(pageResult.getTotal());
        voIPage.setPages(pageResult.getPages());
        voIPage.setCurrent(pageResult.getCurrent());
        voIPage.setSize(pageResult.getSize());
        return voIPage;

    }

    private TBorderLatestNsDetailsVo mapToTBorderLatestStatsVo(TBworderLstestNsDetails tBworderLstestNsDetails, String time) {
        TBorderLatestNsDetailsVo vo = new TBorderLatestNsDetailsVo();
        BeanUtils.copyProperties(tBworderLstestNsDetails, vo);

        String orderType = tBworderLstestNsDetails.getOrderType();
        switch (orderType) {
            case "cp":
                vo.setOrderType("投诉工单");
                break;
            case "install":
                vo.setOrderType("装机工单");
                break;
            default:
                break;
        }

        vo.setDataTime(time);
        return vo;
    }
}




